package dao;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.http.Part;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
    private static final String url = "jdbc:mysql://127.0.0.1:3306/BlogSystem?characterEncoding=utf8&useSSL=false";
    private static  final String user = "root";
    private static  final  String passWord = "123456";
    private static DataSource dataSource = null;
    private static DataSource getSource(){
        if (dataSource == null){
            dataSource = new MysqlDataSource();
            ((MysqlDataSource)dataSource).setURL(url);
            ((MysqlDataSource)dataSource).setUser(user);
            ((MysqlDataSource)dataSource).setPassword(passWord);
        }
        return dataSource;
    }
    public static Connection getCon() throws SQLException {
        return getSource().getConnection();
    }
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
        if (connection != null){
            connection.close();
        }
        if (preparedStatement != null){
            preparedStatement.close();
        }
        if (resultSet != null){
            resultSet.close();
        }
    }
}
